package cn.wolfcode.service.Impl;

import cn.wolfcode.domain.Student;
import cn.wolfcode.mapper.StudentMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Collections;
import java.util.List;

@Transactional
@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public void insert(Student student) {
            studentMapper.insert(student);
    }

    @Override
    public void delete(Long id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public Student selectOne(Long id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        return student;
    }

    @Override
    public List<Student> selectList() {
        List<Student> students = studentMapper.selectAll();
        return students;
    }

    @Override
    public PageResult<Student> query(QueryObject qo) {
        int totalCount = studentMapper.queryForCount(qo);
        if(totalCount==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.emptyList());
        }
        List<Student> students = studentMapper.queryForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount,students);
    }
}
